<template>
  <div class="search-input-wrapper flex f-ai-c">
    <slot name="icon"></slot>
    <input
      class="input"
      :value="key_word"
      :placeholder="placeholder"
      @input="handleInput"
    />
    <div class="icon-wrapper flex f-jc-jc f-ai-c" @click="handleClearInput">
      <icon type="circle-cross" v-if="showClear"></icon>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Icon } from "@nutui/nutui";
Icon.install(Vue);
export default {
  name: "SearchInput",
  components: {
    Icon
  },
  props: {
    placeholder: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      key_word: ""
    };
  },
  methods: {
    handleInput(e) {
      const value = e.target.value;
      this.key_word = value;
    },
    handleClearInput() {
      this.key_word = "";
    }
  },
  watch: {
    key_word(value) {
      this.$emit("handleInput", value);
    }
  },
  computed: {
    showClear() {
      return this.key_word.length > 0;
    }
  }
};
</script>

<style scoped>
.search-input-wrapper {
  padding: 0 10px;

  width: 190px;
  height: 32px;

  box-sizing: border-box;
  border-radius: 20px;
  background: white;
}
.icon-xingqiu {
  font-size: 24px;
  color: gray;
}
.input {
  outline: none;
  border: none;

  box-sizing: border-box;
  padding-left: 5px;
  width: 130px;
  height: 26px;

  background: transparent;
  color: #333;
  font-size: 12px;
}
.input::placeholder {
  color: gainsboro;
}
.icon-wrapper {
  width: 20px;
  height: 20px;
}
</style>
